package models

import (
	"github.com/astaxie/beego/orm"
	"fmt"
	"strconv"
)


//查询用户struct
type AUser struct {
	AdminUser
	RoleName string `orm:"column(name)"`
}


//获取用户信息
func (*AdminUser) GetList(pageNo,pageSize int, beginTime,endTime,userName string) ([]AUser,int,error) {
	var count int
	var adminUser []AUser
	var sqlTemplet string
	countSql := "Select count(1) count from admin_user u left join admin_role r on u.role_id = r.id"
	rowSql := "Select u.* , r.name  from admin_user u left join admin_role r on u.role_id = r.id"
	lastSql := " order By u.create_time desc limit "+ strconv.Itoa((pageNo - 1) * pageSize) + ","+ strconv.Itoa(pageSize)
	if beginTime != "" || userName != "" {
		sqlTemplet = " where 1=1 "
		if beginTime != "" && endTime != "" {
			sqlTemplet += " and u.create_time  >  str_to_date('"+ beginTime +"','%Y-%m-%d') and u.create_time < str_to_date('"+ endTime +"','%Y-%m-%d')"
		}
		if userName != "" {
			sqlTemplet += " and u.user_account like '%"+userName+"%'"
		}
	}
	fmt.Println(rowSql + sqlTemplet + lastSql)
	//获取总条数
	err := orm.NewOrm().Raw(countSql + sqlTemplet).QueryRow(&count)
	if err != nil {
		return nil,0,err
	}
	if count == 0 {
		return nil,0,nil
	}
	_,err = orm.NewOrm().Raw(rowSql + sqlTemplet + lastSql).QueryRows(&adminUser)
	if err != nil {
		return nil,0,err
	}
	return adminUser,count,nil
}

